//
//  NumberType+ZMExtension.swift
//  TravelForUser
//
//  Created by Rowling on 2020/9/9.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import Foundation
extension Double : ZMCompatible{}
extension Int : ZMCompatible{}

extension ZM where Base == Double  {
    func timeStr(_ format:String) -> String {
        let date = toDate()
        let formatter = DateFormatter()
        formatter.dateFormat = format
        formatter.locale = Locale.init(identifier: "en_US_POSIX")
        formatter.calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
        let str = formatter.string(from: date)
        return str
    }
    
    func toDate() -> Date {
        let timeInterval  = String(format: "%.f", base).count == 10 ? base : base / 1000
        return Date(timeIntervalSince1970: timeInterval)
    }
    
    func toNSDate() -> NSDate {
        return toDate() as NSDate
    }
    
    func toInt() -> Int {
        return Int(base)
    }
    
    func toString() -> String {
        return "\(base)"
    }
    
    
    func toString(keep:Int,_ realKeep : Bool = false) -> String {
//        var rs = String(format:"%.\(keep)f",base)
//        if realKeep {
//            return rs
//        }
//        if !rs.contains("."){return rs}
//        let spStr = rs.split(separator: ".")
//        if spStr.count > 0 , let last = spStr.last {
//            var needDrop = 0
//            for c in last.reversed() {
//                if c == "0" {
//                   needDrop += 1
//                } else {
//                    break
//                }
//            }
//            if needDrop > 0 {
//                if needDrop == keep {
//                    needDrop += 1
//                }
//                rs = String(rs.dropLast(needDrop))
//            }
//        }
        let d = NSDecimalNumber.init(value: base)
        let r = d.rounding(accordingToBehavior: ZMUtil.DecimalNumberHandler(roundingMode: .plain, scale: Int16(keep)))
        return r.stringValue
//        return rs
    }
    
    func xmToString(keep:Int, mode : NSDecimalNumber.RoundingMode = .down) -> String {
        let d = NSDecimalNumber.init(value: base)
        let r = d.rounding(accordingToBehavior: ZMUtil.DecimalNumberHandler(roundingMode: mode, scale: Int16(keep)))
        return r.stringValue
    }
    
    /// 单位转换 --> k， w
    func dealNum() -> String {
        if base >= 10000 {
            return (base / 10000.0).zm.toString(keep: 0) + "W"
//        } else if base >= 1000 {
//            return (base / 1000.0).zm.toString(keep: 0) + "K"
        } else {
            return base.zm.toString(keep: 2)
        }
    }
    // double小数截断
    func roundingDown(keep : Int16) -> String {
        let handle = ZMUtil.DecimalNumberHandler(roundingMode: .down, scale: keep)
        
        let r = NSDecimalNumber.init(value: base).rounding(accordingToBehavior: handle)
        
        return r.stringValue
    }
}

extension ZM where Base == Int  {
    var double : Double {
        get {Double(base)}
        set{}
    }
    
    var decimal : NSDecimalNumber {
        get {
            return NSDecimalNumber.init(value: base)
        }
    }
}

